﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;

namespace WebToolsClslib
{
    public class MyMembership:IDisposable
    {
        public bool validate(string username, string pwd, HttpResponse response)
        {
            if (1 == 1)//Membership.ValidateUser(username, pwd))
            {
                FormsAuthentication.SetAuthCookie(username, true, FormsAuthentication.FormsCookiePath);

                FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, username, DateTime.Now, DateTime.Now.AddMinutes(20), false, username);
                // generate new identity
                FormsIdentity identity = new FormsIdentity(ticket);
                HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(ticket));
                cookie.Expires = DateTime.Now.AddHours(0.5);
                //cookie.Value = username;
                // write to client.
                response.Cookies.Add(cookie);
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool SignOut()
        {
            FormsAuthentication.SignOut();
            HttpContext.Current.Session.Abandon();
            return true;
        }

        public void NoneCache(HttpResponse response) 
        {
            //asp 做法
            response.Buffer = true;
            response.ExpiresAbsolute = DateTime.Now.AddDays(-1);
            response.Expires = 0;
            response.CacheControl = "no-cache";
            

            //response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);

            response.Cache.SetCacheability(HttpCacheability.NoCache);
            response.Expires = -1;
            response.AddHeader("pragma", "no-cache");
            response.AddHeader("cache-control", "no-cache");
            response.CacheControl = "no-cache";
        }

        public void Dispose()
        {
           
        }
    }
}